from shot import Shot
from point import Point
import pygame
from pygame.locals import *

class Spaceship:
    point = None
    image = None
    key_rotate = None
    key_move = None

    def __init__(self, point = Point() ):
        self.image = pygame.image.load('spaceship.png').convert()
        self.image.set_colorkey(self.image.get_at((0,0)))
        self.point = point
        
    def shoot( self ):
        shot = Shot(Point())
        shot.point.angle = self.point.angle
        shot.point.set_velocity( 20 )
        shot.point.x = self.point.x
        shot.point.y = self.point.y
        return shot
        
        
    def paint(self,screen):
        image_to_paint = pygame.transform.rotate(self.image,self.point.angle)
        screen.blit( image_to_paint, (self.point.x, self.point.y))
        
    def handle_keydown(self, event ):
        if event.key == K_SPACE:
            return self.shoot()      
        if event.key == K_RIGHT:
            self.key_rotate = K_RIGHT
        if event.key == K_LEFT:
            self.key_rotate = K_LEFT
        if event.key == K_UP:
            self.key_move = K_UP
        if event.key == K_DOWN:
            self.key_move = K_DOWN
        
            

            
    def handle_keyup(self, event):
        if event.key == K_RIGHT or event.key == K_LEFT:
            self.key_rotate = None
        if event.key == K_UP or event.key == K_DOWN: 
            self.key_move = None
            

            
    def move (self):
        if self.key_rotate == K_RIGHT:
            self.point.rotate_right()
        if self.key_rotate == K_LEFT:
            self.point.rotate_left()
        if self.key_move == K_UP:
            self.point.speed_up()
        if self.key_move == K_DOWN:
            self.point.speed_down()
        
            
        self.point.move()
            
    